package com.carbon.trade.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.trade.entity.CarbonTradeContract;
import com.carbon.trade.param.IntendedTransactionParam;
import com.carbon.trade.service.CarbonTradePriceService;
import com.carbon.trade.param.CarbonTradePriceQueryParam;
import com.carbon.trade.vo.CarbonTradePriceQueryVo;
import com.carbon.trade.entity.CarbonTradePrice;
import com.carbon.trade.common.BaseController;
import com.carbon.domain.common.ApiResult;
import com.carbon.common.api.Paging;
import com.carbon.trade.vo.CarbonTradePriceReturnVo;
import com.carbon.trade.vo.CarbonTradeQuoteReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 碳交易询报价 前端控制器
 * </p>
 *
 * @author lin rizhao
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/carbonTradePrice")
@Api(value = "碳交易询报价模块", tags = {"碳交易询报价模块"})
public class CarbonTradePriceController extends BaseController {

    @Autowired
    private CarbonTradePriceService carbonTradePriceService;
    @PostMapping("/add")
    public ApiResult add(@RequestBody @Valid CarbonTradePrice carbonTradePrice){
        carbonTradePrice.setTradeQuoteId(159L);
            carbonTradePrice.setBuyerDeliveryExchange(carbonTradePriceService.transactionex(carbonTradePrice.getBuyerDeliveryExchange()));
            carbonTradePrice.setBuyerDeliveryMethod(carbonTradePriceService.transactionex(carbonTradePrice.getBuyerDeliveryMethod()));
            return ApiResult.result(carbonTradePriceService.save(carbonTradePrice));

    }
    @PostMapping("/addB")
    public ApiResult addB(@RequestBody @Valid CarbonTradePrice carbonTradePrice){
        carbonTradePrice.setTradeQuoteId(159L);
            carbonTradePrice.setSellerDeliveryExchange(carbonTradePriceService.transactionex(carbonTradePrice.getSellerDeliveryExchange()));
            carbonTradePrice.setSellerDeliveryMethod(carbonTradePriceService.transactionex(carbonTradePrice.getSellerDeliveryMethod()));
            carbonTradePrice.setAssetType(carbonTradePriceService.transactionex(carbonTradePrice.getAssetType()));
            carbonTradePrice.setProjectType(carbonTradePriceService.transactionex(carbonTradePrice.getProjectType()));
            return ApiResult.result(carbonTradePriceService.save(carbonTradePrice));

    }
    @PostMapping("/handleSearch")
    public ApiResult<CarbonTradePriceReturnVo> handleSearch(@RequestBody CarbonTradePriceQueryParam param){
        Page<CarbonTradePrice> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        param.setAssetType(carbonTradePriceService.transactionex(param.getAssetType()));
        IPage<CarbonTradePrice> paging = carbonTradePriceService.handleSearch(page,param);
        CarbonTradePriceReturnVo carbonTradePriceReturnVo = new CarbonTradePriceReturnVo();
        carbonTradePriceReturnVo.setList(paging.getRecords());
        for (int i = 0; i < carbonTradePriceReturnVo.getList().size(); i++) {
            carbonTradePriceReturnVo.getList().get(i).setAssetType(carbonTradePriceService.transactionNumberToChinese(carbonTradePriceReturnVo.getList().get(i).getAssetType()));
            carbonTradePriceReturnVo.getList().get(i).setProjectType(carbonTradePriceService.transactionNumberToChinese(carbonTradePriceReturnVo.getList().get(i).getProjectType()));
            String s = carbonTradePriceService.transactionNumberToChinese(carbonTradePriceReturnVo.getList().get(i).getSellerDeliveryMethod());
            carbonTradePriceReturnVo.getList().get(i).setSellerDeliveryMethod(s);
            carbonTradePriceReturnVo.getList().get(i).setTradeRole(carbonTradePriceReturnVo.getList().get(i).getBuyerId()==param.getTenantId()?"0270000001":"0270000002");
            carbonTradePriceReturnVo.getList().get(i).setSellerDeliveryExchange(carbonTradePriceService.transactionNumberToChinese(carbonTradePriceReturnVo.getList().get(i).getSellerDeliveryExchange()));
        }
        carbonTradePriceReturnVo.setTotal((int) paging.getTotal());
        return ApiResult.ok(carbonTradePriceReturnVo);
    }


}

